//
//  AddGroupVC.swift
//  YueChat
//
//  Created by IMac on 2024/9/20.
//

import UIKit

class AddGroupVC: BaseVC {
    var group = DMCCGroup()
    var desc = String()
    init(group: DMCCGroup = DMCCGroup()) {
        super.init(nibName: nil, bundle: nil)
        self.group = group
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setup()
        
        self.updateGroup()
    }
    
    func updateGroup () {
        Task {
            if let group = await gosnGroup.fetchGroupInfo(self.group.groupId) {
                
                await MainActor.run {
                    GK_main_async_safe {
                        self.group = group
                        self.tableView.reloadData()
                    }
                    
                }
            }
        }
    }
    
    func joinBtnClick() {
        MHAlert.showLoading()
        Task {
            let res = await gosnGroup.joinGroup(group.groupId, desc, "JoinGroup")
            MHAlert.showMessage(KLocalizedString("GroupMessageBeenSended"))
            await MainActor.run {
                
                if (res != nil) {
                    MHAlert.showLoadingStr(KLocalizedString("CallActionInvite"))
                    self.navigationController?.popToRootViewController(animated: true)
                }else{
                    MHAlert.showMessage(KLocalizedString("GroupMessageTimeOut"))
                }
            }
            
        }
    }
    
    @objc func tfTarget(tf:UITextField) {
        desc = tf.text!
    }
    
    func setup () {
        tableView.register(UINib(nibName: "UserCell", bundle: nil), forCellReuseIdentifier: "UserCell")
        tableView.register(UINib(nibName: "LogoutCell", bundle: nil), forCellReuseIdentifier: "LogoutCell")
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 && indexPath.row == 0 {
            return 120
        }
        
        return 60
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
        if indexPath.section == 0  {
            let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath) as! UserCell
            cell.setGroup(group: self.group)
            cell.imgWidth.constant = 70
            cell.imgHeight.constant = 70
            return cell
        }else if (indexPath.section == 1) {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
            cell.contentView.backgroundColor = UIColor.clear
//            cell.accessoryType = .disclosureIndicator
            let tf = UITextField(frame: CGRectMake(20, 10, kScreenWidth - 40, 40))
            tf.placeholder = "附言/验证密码"
//            tf.delegate = self
            tf.addTarget(self, action: #selector(tfTarget), for: .editingChanged)
            cell.contentView.addSubview(tf)
            return cell
        }

        let cell = tableView.dequeueReusableCell(withIdentifier: "LogoutCell", for: indexPath) as! LogoutCell
        cell.titleLab.text = "加入群聊"
        cell.titleLab.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
        cell.titleLab.textColor = UIColor.MainColor
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        if (indexPath.section == 2) {
            joinBtnClick()
        }
    }
}
